fn div(a: i32, b: i32) -> Result<f64, String> {
    if b == 0 {
        Err(String::from("Cannot divide by zero"))
    } else {
        Ok(a as f64 / b as f64)
    }
}

fn find(arr: &[i32], e: i32) -> Option<usize> {
    for (i, v) in arr.iter().enumerate() {
        if *v == e {
            return Some(i);
        }
    }
    None
}

fn main() {
    // result
    match div(10, 2) {
        Ok(v) => println!("{}", v),
        Err(e) => println!("Error: {}", e),
    }

    // option
    let arr = [1, 3, 5, 7, 9];
    match find(&arr, 12) {
        Some(i) => println!("Found at index {}", i),
        None => println!("Not found"),
    }

    // 对Result<T, E>和Option<T>进行提取的方法
    // unwarp()，遇到Err(E)h或None会调用panic，导致系统崩溃
    let ok: Result<i32, &str> = Ok(1);
    let result = ok.unwrap();
    println!("unwrap: {}", result);

    // unwrap_or()，遇到Err(E)或None会返回，方法传入的默认值
    let err: Result<i32, &str> = Err("panic!");
    let result = err.unwrap_or(0);
    println!("unwrap_or: {}", result);

    // unwrap_or_else()，遇到Err(E)或None会执行方法传入的闭包，err参数是报错内容（对None进行处理时闭包没有参数），最终抛出闭包返回值
    let err: Result<i32, &str> = Err("panic!");
    let result = err.unwrap_or_else(|err| {
        println!("error message: {}", err);
        1
    });
    println!("unwrap_or_else: {}", result);

    // expect()，在遇到Err(E)或Noneh会调用，panic将提示传入的内容
    let err: Result<i32, &str> = Err("error message!");
    err.expect("panic!");
}
